package www.yingdi365.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;
import com.zhy.magicviewpager.transformer.ScaleInTransformer;

import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;
import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseNotLeftActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.fragment.QingJianYLFragment;
import www.yingdi365.com.model.AppMagazineSave;
import www.yingdi365.com.model.AppMagazineSaveResult;
import www.yingdi365.com.model.AppMagazineTmpl;
import www.yingdi365.com.model.AppMagazineTmplData;
import www.yingdi365.com.model.OkObject;
import www.yingdi365.com.util.DpUtils;
import www.yingdi365.com.util.GsonUtils;
import www.yingdi365.com.util.LogUtil;
import www.yingdi365.com.util.ScreenUtils;

public class ZaZhiYLActivity extends ZjbBaseNotLeftActivity implements View.OnClickListener {

    private ViewPager id_viewpager;
    private AppMagazineTmpl tmplsBean;
    private List<AppMagazineTmplData.PagesBean> pagesBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qing_jian_yl);
        init();
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        tmplsBean = (AppMagazineTmpl) intent.getSerializableExtra(Constant.INTENT_KEY.TmplsBean);
        pagesBeanList = tmplsBean.getTmplDataEntity().getPages();
    }

    @Override
    protected void findID() {
        id_viewpager = (ViewPager) findViewById(R.id.id_viewpager);
    }

    @Override
    protected void initViews() {
        int screenWidth = ScreenUtils.getScreenWidth(this);
        ViewGroup.LayoutParams layoutParams = id_viewpager.getLayoutParams();
        float width = screenWidth-DpUtils.convertDpToPixel(100f, this);
        layoutParams.width = (int) width;
        layoutParams.height = (int) (width*1.4375f);
        id_viewpager.setLayoutParams(layoutParams);
        id_viewpager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        id_viewpager.setPageMargin((int) DpUtils.convertDpToPixel(20f,this));
        id_viewpager.setOffscreenPageLimit(3);
        id_viewpager.setPageTransformer(true, new ScaleInTransformer());
        ((TextView) findViewById(R.id.textViewTitle)).setText("杂志预览");
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.buttonLiJiZZ).setOnClickListener(this);
        findViewById(R.id.imageBack).setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }
    
    private OkObject getOkObject() {
        String url = Constant.HOST +Constant.Url.AppMagazineService+"magazine/save";
        HttpHeaders headers = new HttpHeaders();
        headers.put(Constant.TOKEN, appLoginResult.getToken());
        HashMap<String, String> params = new HashMap<>();
        return new OkObject(headers,params, url);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageBack:
                finish();
                break;
            case R.id.buttonLiJiZZ:
                showLoadingDialog();
                AppMagazineSave appMagazineSave = new AppMagazineSave(
                        tmplsBean.getMusic(),
                        tmplsBean.getName(),
                        tmplsBean.getTmplData(),
                        tmplsBean
                );
                LogUtil.LogShitou("ZaZhiYLActivity--杂志保存", ""+GsonUtils.parseObject(appMagazineSave));
                OkObject okObject = getOkObject();
                OkGo.post(okObject.getUrl())//
                        .tag(this)//
                        .headers(okObject.getHeaders())
                        .upJson(GsonUtils.parseObject(appMagazineSave))//
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                cancelLoadingDialog();
                                LogUtil.LogShitou("ZaZhiYLActivity--杂志保存返回", ""+s);
                                try {
                                    AppMagazineSaveResult appMagazineSaveResult = GsonUtils.parseJSON(s, AppMagazineSaveResult.class);
                                    if (appMagazineSaveResult.isSuccess()) {
                                        Intent intent = new Intent();
                                        intent.setClass(ZaZhiYLActivity.this, BianJiZZYLActivity.class);
                                        intent.putExtra(Constant.INTENT_KEY.ID, appMagazineSaveResult.getContent().getId());
                                        startActivity(intent);
                                        finish();
                                    } else {
                                        Toast.makeText(ZaZhiYLActivity.this, appMagazineSaveResult.getMessage(), Toast.LENGTH_SHORT).show();
                                    }
                                } catch (Exception e) {
                                    Toast.makeText(ZaZhiYLActivity.this, getResources().getString(R.string.shujuchucuo), Toast.LENGTH_SHORT).show();
                                }
                            }
                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                                cancelLoadingDialog();
                                if (response!=null){
                                    if (response.code()==401){
                                        showReLoginDialog();
                                    }
                                }
                                Toast.makeText(ZaZhiYLActivity.this, getResources().getString(R.string.qingqiushibai), Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
        }
    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            String preview = pagesBeanList.get(position).getPreview();
            return new QingJianYLFragment(preview);
        }

        @Override
        public int getCount() {
            return pagesBeanList.size();
        }
    }
}
